<%--
  Created by IntelliJ IDEA.
  User: 91658
  Date: 2018/6/8
  Time: 10:20
  记账管理
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>记账管理</title>
    <%@ include file="../common/common.jsp" %>
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <%@ include file="../common/head.jsp" %>
        <%@ include file="../common/left.jsp" %>
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <div style="padding: 15px;">
                <div id="formDiv">
                    <form id="chargeForm" class="layui-form" onsubmit="return false;">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">账户类型</label>
                                <div class="layui-input-inline">
                                    <select name="accountType" lay-search="" id="accountType">
                                        <option value="">直接选择或搜索选择</option>
                                        <c:forEach items="${accountList}" var="account">
                                            <option value="${account.id}">${account.accountName}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">支出类型</label>
                                <div class="layui-input-inline">
                                    <select name="spendingType" lay-search="" id="spendingType">
                                        <option value="">直接选择或搜索选择</option>
                                        <c:forEach items="${spendingList}" var="spending">
                                            <option value="${spending.id}">${spending.spendingName}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">收入类型</label>
                                <div class="layui-input-inline">
                                    <select name="incomeType" lay-search="" id="incomeType">
                                        <option value="">直接选择或搜索选择</option>
                                        <c:forEach items="${incomeList}" var="income">
                                            <option value="${income.id}">${income.incomeName}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">类型</label>
                                <div class="layui-input-inline">
                                    <input type="radio" name="paymentsType" value="1" title="支出" checked="">
                                    <input type="radio" name="paymentsType" value="2" title="收入">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">创建时间</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="beginTime" id="beginTime" placeholder="开始时间" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <input type="text" name="endTime" id="endTime" placeholder="结束时间" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn" lay-filter="demo1" id="formBtn">查询</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                <button class="layui-btn" id="importBtn">导入</button>
                                <button class="layui-btn" id="addCharge">新增</button>
                            </div>
                        </div>
                    </form>
                </div>

                <div id="tableDiv">
                    <table class="layui-hide" id="chargeTable" lay-filter="chargeTable"></table>
                </div>
            </div>
        </div>
        <%@ include file="../common/bottom.jsp" %>
    </div>
</body>
<script type="text/javascript">
    layui.use(['form', 'laydate', 'table', 'upload', 'layer', 'laypage'], function(){
        var $ = layui.jquery;
        var form = layui.form
            ,laydate = layui.laydate
            ,table = layui.table
            ,upload = layui.upload
            ,layer = layui.layer
            ,laypage = layui.laypage;

        form.render('select');
        //日期
        laydate.render({
            elem: '#beginTime'
        });
        laydate.render({
            elem: '#endTime'
        });
        table.render({
            elem: '#chargeTable'
            ,url:'/charge/charge_record/'
            ,method: 'POST'
            ,cols: [[
                {field:'paymentsName', width:130, title: '类型', sort: true}
                ,{field:'projectName', width:130, title: '类别'}
                ,{field:'money', width:130, title: '金额', sort: true}
                ,{field:'accountName', width:130, title: '账户'}
                ,{field:'accountBalance', width:130, title: '账户余额'}
                ,{field:'createTime', width: 200, title: '时间'}
                ,{field:'remark', title: '备注'}
                ,{fixed: 'right', title: '操作', width:150, align:'center', toolbar: '#toolBar'} //这里的toolbar值是模板元素的选择器
            ]]
            ,response: {
                statusCode: 1 //成功的状态码，默认：0
            }
            ,request: {
                pageName: 'pageNo' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
//            ,where: {
//                pageNo: 1
//                , pageSize: 10
//            }
            ,page: true
            ,done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);

                //得到当前页码
                console.log(curr);

                //得到数据总量
                console.log(count);
            }
        });
        $("#formBtn").click(function () {
            table.reload('chargeTable', {
                where: {
                    accountType: $("#accountType").val()
                    ,spendingType: $("#spendingType").val()
                    ,incomeType: $("#incomeType").val()
                    ,paymentsType: $("input[name='paymentsType']:checked").val()
                    ,beginTime: $("#beginTime").val()
                    ,endTime: $("#endTime").val()
                }
            });
        });

        //监听工具条
        table.on('tool(chargeTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                console.log(obj);
                //同步更新缓存对应的值
                obj.update({
                    username: '123'
                    ,title: 'xxx'
                });
            }
        });

        //执行实例
        var uploadInst = upload.render({
            elem: '#importBtn' //绑定元素
            ,url: '/charge/upload' //上传接口
            ,accept: 'file'
            ,acceptMime: 'application/msexcel'
            ,exts: 'xls|xlsx'
            ,done: function(res){
                //上传完毕回调
                table.reload('chargeTable');
            }
            ,error: function(){
                //请求异常回调
            }
        });

        $("#addCharge").click(function () {
            console.log(1);
            window.location.href = "/charge/add_charge_frame";

        });
    });
</script>
<script type="text/html" id="toolBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</html>
